'use strict';
/**
* 定义 ngDialog 模板
*/
angular.module('ngDialogTemplate',['template/ngDialog/tips.html'])
angular.module("template/ngDialog/tips.html", []).run(["$templateCache", function($templateCache) {
  $templateCache.put("template/ngDialog/tips.html",
	'\
			<p ng-bind-html="content"></p>\
			<div class="ngdialog-buttons">\
					<button type="button" class="ngdialog-button ngdialog-button-secondary" ng-click="closeThisDialog(0)">关闭</button>\
			</div>'+
    "");
}]);

angular.module('utils', ['ngDialog']).service('signature', function(appid,key,md5) {
    // var appid = 'H5';
    // var key = 'innjia2016111703';
    // 对象键值排序
		function orderObj(obj) {
			let o = {};
			Object.keys(obj).forEach((key) => {
				o[key] = obj[key];
			});
			return o;
		};
		// 对象按`name1=value1&name2=value2`序列化
		function param(obj) {
			let arr = [];
			for (let i in obj) {
				arr.push(i + '=' + obj[i]);
			}
			return arr.join('&');
		};
    this.run = function(methodId, params) {
        var result = {
            appid: appid,
            methodId: methodId + "",
            methodParam: JSON.stringify(params),
            nonce: Math.random().toString(),
            timestamp: new Date().getTime().toString()
        }
        // MD5字符串参数
  			var str = decodeURIComponent(param(orderObj(result)));
  			str += '&key=' + key;
        var hash = md5.SparkMD5.hash(str).toUpperCase();
        result.signature = hash;
        return result;
    }
}).service('dialog', function(ngDialog) {

	this.tips = function(content){
		if(!content){
			return;
		}
		ngDialog.openConfirm({
			templateUrl:'template/ngDialog/tips.html',
			data:{
				content:content
			},
			controller: function($scope){
				var options = angular.copy($scope.ngDialogData, {});
				$scope.content = options.content
			},
			transclude:true,
	    replace:true,
		})
	}

}).factory('common', function() {
    var _common = {
        /**
         * 获取一个随机的ID
         * @type {[String]}
         */
        getCustomID: _getCustomID,
        /**
         * 时间格式化
         */
        formatDate: function(date) {
            var dM, darr, dd, de, dh, dmm, ds, dy;
            de = new Date(date);
            darr = [];
            dy = de.getFullYear();
            darr.push(dy);
            darr.push("-");
            dM = de.getMonth() + 1;
            dM = dM >= 10 ? dM : '0' + dM;
            darr.push(dM);
            darr.push("-");
            dd = de.getDate();
            dd = dd.toString().length < 2 ? '0' + dd : dd;
            darr.push(dd);
            darr.push(" ");
            dh = de.getHours();
            dh = dh.toString().length < 2 ? '0' + dh : dh;
            darr.push(dh);
            darr.push(":");
            dmm = de.getMinutes();
            dmm = dmm.toString().length < 2 ? '0' + dmm : dmm;
            darr.push(dmm);
            darr.push(":");
            ds = de.getSeconds();
            ds = ds.toString().length < 2 ? '0' + ds : ds;
            darr.push(ds);
            return darr.join("");
        }
    }
    return _common;

    function _getCustomID(str) {
      var base = str || '';
      var mr = base + (Math.random().toString().substr('3', 7));
      return mr;
    }
});
